package com.flyco.modules.sms.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.flyco.modules.sms.entity.SmsCouponStore;
import com.flyco.modules.sms.mapper.SmsCouponStoreMapper;
import com.flyco.modules.sms.model.SmsCouponGrantModel;
import com.flyco.modules.sms.service.ISmsCouponStoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 指定门店券
 * @Author: flyco
 * @Date:   2020-09-28
 * @Version: V1.0
 */
@Service
public class SmsCouponStoreServiceImpl extends ServiceImpl<SmsCouponStoreMapper, SmsCouponStore> implements ISmsCouponStoreService {

    @Autowired
    private SmsCouponStoreMapper smsCouponStoreMapper;

    @Override
    public boolean deleteByMainId(Long mainId) {
        return smsCouponStoreMapper.deleteByMainId(mainId);
    }

    @Override
    public List<SmsCouponStore> selectByMainId(Long mainId) {
        return smsCouponStoreMapper.selectByMainId(mainId);
    }


    @Override
    public Page<SmsCouponStore> getCouponStorePage(Page<SmsCouponStore> page, Wrapper<SmsCouponStore> wrapper) {
        return page.setRecords(smsCouponStoreMapper.getCouponStorePage(page, wrapper));
    }

    @Override
    public void addCouponStore(SmsCouponGrantModel smsCouponGrantModel) {
        List<SmsCouponStore> couponStores = new ArrayList<>();
        for (Long storeId : smsCouponGrantModel.getStoreIdList()){
            SmsCouponStore couponStore = new SmsCouponStore();
            couponStore.setCouponId(smsCouponGrantModel.getCouponId());
            couponStore.setStoreId(storeId);
            couponStores.add(couponStore);
        }
        this.saveBatch(couponStores);
    }

    @Override
    public void editCouponStore(SmsCouponGrantModel smsCouponGrantModel) {
        LambdaQueryWrapper<SmsCouponStore> couponStoreQuery = new LambdaQueryWrapper<>();
        couponStoreQuery.eq(SmsCouponStore::getCouponId,smsCouponGrantModel.getCouponId());
        this.remove(couponStoreQuery);
        this.addCouponStore(smsCouponGrantModel);
    }

}
